package com.orange.agent.rebangTody.convert;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.orange.agent.rebangTody.request.HotNewsApiReq;
import com.orange.agent.rebangTody.response.HotNewsApiRes;
import com.orange.bean.core.constant.CacheConstants;
import com.orange.bean.rebangTody.dto.request.HotNewsReq;
import com.orange.bean.rebangTody.dto.response.HotNewsDto;
import com.orange.bean.rebangTody.dto.response.HotNewsRes;
import com.orange.common.utils.ConvertUtils;
import com.orange.framework.consumer.convert.ConsumerConverter;
import org.mapstruct.Mapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2025/9/3 17:53
 */
@Mapper(componentModel = "spring")
public interface HotNewsConverter extends ConsumerConverter<HotNewsApiReq, HotNewsReq, HotNewsApiRes, HotNewsRes> {

    @Override
    default HotNewsApiReq convertReq(HotNewsReq request) {
        HotNewsApiReq hotNewsApiReq = new HotNewsApiReq();
        if (request == null) {
            return hotNewsApiReq;
        }
        BeanUtil.copyProperties(request, hotNewsApiReq);
        return hotNewsApiReq;
    }


    @Override
    default HotNewsRes convertRes(HotNewsApiRes response) {
        HotNewsRes hotNewsRes = new HotNewsRes();
        if (response == null) {
            return hotNewsRes;
        }
        hotNewsRes.setCurrentPage(response.getCurrentPage());
        hotNewsRes.setTotalPage(response.getTotalPage());
        String list = response.getList();
        List<HotNewsDto.News> newsListRes =
                ConvertUtils.jsonToList(list, new TypeReference<List<HotNewsDto.News>>() {
                });
        // List转换
        List<HotNewsRes.News> newsPageRes = newsListRes.stream().map(item -> {
            HotNewsRes.News newsList = new HotNewsRes.News();
            newsList.setIds(item.getIds());
            newsList.setTitle(item.getTitle());
            newsList.setDesc(item.getDesc());
            newsList.setType(item.getType());
            newsList.setTypeN(CacheConstants.NEWS_TYPE.get(item.getType()));
            newsList.setDetailUrl(item.getDetailUrl());
            newsList.setHotNum(item.getHotNum());
            return newsList;
        }).collect(Collectors.toList());
        hotNewsRes.setList(newsPageRes);
        return hotNewsRes;
    }
}
